package com.gp.cyg.task;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * MyDelay
 *
 * @author gsp
 * @date 2022/9/17
 */
public class MyDelay implements Delayed {
    private String name;
    private long start = System.currentTimeMillis();
    private long time;

    public MyDelay(String name, long time) {
        this.name = name;
        this.time = time;
    }

    /**
     * 需要实现的接口，获得延迟时间   用过期时间-当前时间
     *
     * @param unit
     * @return
     */

    public long getDelay(TimeUnit unit) {
        return unit.convert((start + time) - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    /**
     * 用于延迟队列内部比较排序   当前时间的延迟时间 - 比较对象的延迟时间
     *
     * @param o
     * @return
     */
    public int compareTo(Delayed o) {
        MyDelay o1 = (MyDelay) o;
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

    @Override
    public String toString() {
        return "MyDelayedTask{" +
                "name='" + name + '\'' +
                ", time=" + time +
                '}';
    }

    public static void main(String[] args) {
//        HttpRequest get = HttpUtil.createGet("http://cyg.changyou.com/details/?goodsCode=1622209151309320189");
//        System.out.println(get.execute());

       // 1192994106




    }
}
